<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>用户登录入住</title>
</head>
<body>
<h1>用户登录入住</h1>

<%
    //加载驱动       
    Class.forName("org.mariadb.jdbc.Driver");

    // 数据库连接信息
    String dbURL = "jdbc:mariadb://10.220.140.102:3366/studb11";
    String dbUser = "stua";
    String dbPassword = "zHZUA65r";

    // 从请求中获取用户名和密码
    String inputUsername = request.getParameter("name");
    String inputPassword = request.getParameter("phone");


    // 建立连接
    Connection conn = DriverManager.getConnection(dbURL, dbUser, dbPassword);

    // 验证用户身份
    String userSql = "SELECT * FROM 成都梦之旅文君楼庭院酒店（宽窄巷子店） WHERE name = ? AND phone = ?";
    PreparedStatement userStmt = conn.prepareStatement(userSql);
    userStmt.setString(1, inputUsername);
    userStmt.setString(2, inputPassword);
    ResultSet rs = userStmt.executeQuery();

    if (rs.next()) {
        // 用户验证成功，分配房间
        String roomSql = "UPDATE 成都梦之旅文君楼庭院酒店（宽窄巷子店） SET status = '已预订' WHERE status = '空置' LIMIT 1";
        Statement roomStmt = conn.createStatement();
        int rowsAffected = roomStmt.executeUpdate(roomSql);

        if (rowsAffected > 0) {
            out.println("登录成功，房间已分配。");
        } else {
            out.println("登录成功，但当前没有空置房间可供分配。");
        }
    } else {
        out.println("登录失败，用户名或密码错误。");
    }

    // 关闭资源
    rs.close();
    userStmt.close();
    conn.close();
%>



<form action="roomAllocation.jsp" method="post">

    <label for="username">用户名:</label>

    <input type="text" id="username" name="username" required><br><br>
    <label for="password">手机号:</label>
    <input type="password" id="password" name="password" required><br><br>
    <input type="submit" value="登录入住">
</form>

</body>
</html>